package com.system.goodsserver.controller;


import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.system.commonutils.bean.ApiResult;
import com.system.commonutils.bean.PagesBean;

import com.system.commonutils.utils.RedisUtil;
import com.system.commonutils.utils.SimpleFormatutiles;
import com.system.goodsserver.bean.*;
import com.system.goodsserver.bus.TestSender;
import com.system.goodsserver.pojo.GoodsBrand;
import com.system.goodsserver.pojo.GoodsCover;
import com.system.goodsserver.pojo.GoodsDetailimg;
import com.system.goodsserver.pojo.GoodsType;
import com.system.goodsserver.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Xuheng Zhou
 * @date 2020/7/7 10:00
 * @desc
 */

@Api(value = "商品管理模块",tags = "后端-商品服务API")
@RestController
@RequestMapping("/admin/GoodsManage")
@DefaultProperties(defaultFallback = "golbalFallback")
public class GoodsController {


    @Autowired()
    RedisUtil redisUtil;

    @Autowired
    TestSender testSender;

    @Autowired
    private GoodsService service;

    /**
     * 默认熔断回调
     * @return
     */
    public ApiResult<Object> golbalFallback(Throwable throwable){
        String localizedMessage = throwable.getLocalizedMessage();
        return ApiResult.failure(localizedMessage);
    }




    @ApiOperation(value = "查询商品列表（包含多条件）/（已调试）")
    @PostMapping(value = "/selcetGoods",produces = "application/json;charset=UTF-8")
    public ApiResult<PagesBean<ResultGoodsSelectBean>> selcetGoods (@RequestBody SelectGoodsParamBean selectGoodsParamBean){



        List<ResultGoodsSelectBean> list=new ArrayList<>();

        //获取查询的数据
        list=service.selcetGoods(selectGoodsParamBean);
        //获取查询的数据总条数
        Integer count=service.selcetGoodsCount(selectGoodsParamBean);

        PagesBean pagesBean=new PagesBean();

        pagesBean.setData(list);
        pagesBean.setPageSize(selectGoodsParamBean.getSize());
        //因为查询所有数据的时候，实体类的引用传递，实体类的page值已经改变了，page已经减1，传回前端，就需要加1。
        pagesBean.setNowsPage(selectGoodsParamBean.getPage()+1);
        pagesBean.setDataCount(count);

        return ApiResult.successWithData("查询成功",pagesBean);
    }


    @ApiOperation(value = "查询所有商品类型列表（已调试）")
    @PostMapping(value = "/selcetGoodsType",produces = "application/json;charset=UTF-8")
    public ApiResult<List<GoodsType>> selcetGoodsType (){

        List<GoodsType> list=service.selcetGoodsType();
        if (list.size()>0){
            return ApiResult.successWithData("查询成功",list);
        }else {
            return ApiResult.failure("查询失败");
        }
    }


    @ApiOperation(value = "查询所有商品品牌（已调试）")
    @PostMapping(value = "/selcetGoodsBrand",produces = "application/json;charset=UTF-8")
    public ApiResult<List<GoodsBrand>> selcetGoodsBrand (){

        List<GoodsBrand> list=service.selcetGoodsBrand();
        if (list.size()>0){
            return ApiResult.successWithData("查询成功",list);
        }else {
            return ApiResult.failure("查询失败");
        }
    }

    @ApiOperation(value = "查询具体商品的详情信息（已调试）")
    @PostMapping(value = "/selcetGoodsOneById",produces = "application/json;charset=UTF-8")
    @ApiImplicitParam(name = "GoodsId",value = "商品id",dataType = "String",required = true)
    public ApiResult<ResultGoodsDetailSelectBean> selcetGoodsOneById (@RequestParam Integer GoodsId){

        ResultGoodsDetailSelectBean resultGoodsDetailSelectBean=service.selcetGoodsOneById(GoodsId);
        if (resultGoodsDetailSelectBean !=null){
            return ApiResult.successWithData("查询成功",resultGoodsDetailSelectBean);
        }else {
            return ApiResult.failure("查询失败");
        }
    }

    @ApiOperation(value = "开启商品规格特价（已调试）")
    @PostMapping(value = "/updateSpecificationsIsPriceBySid",produces = "application/json;charset=UTF-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "SpecificationsId",value = "商品规格id",dataType = "String",required = true),
            @ApiImplicitParam(name = "isPrice",value = "是否特价（0否 1是）",dataType = "Integer"),
            @ApiImplicitParam(name = "price",value = "特价金额（0显示无）",dataType = "Double"),
            @ApiImplicitParam(name = "isCoupon",value = "是否享用优惠卷（0否，1是）",dataType = "Integer"),
            @ApiImplicitParam(name = "endtime",value = "特价到期时间",dataType = "String")

    })
    public ApiResult updateSpecificationsIsPriceBySid (@RequestParam Integer SpecificationsId,
                                                        Integer isPrice,
                                                        Double price,
                                                        Integer isCoupon,
                                                        String endtime){



        Date priceEndtime=SimpleFormatutiles.stringdate(endtime,"yyyy-MM-dd HH:mm:ss");

        boolean tf=service.updateSpecificationsIsPriceBySid(SpecificationsId,isCoupon,isPrice,price, priceEndtime);
        if (tf==true){
            return ApiResult.success("修改规格特价成功！");
        }
        return ApiResult.failure("修改规格特价失败！");
    }


    @ApiOperation(value = "关闭商品规格特价（已调试）")
    @PostMapping(value = "/updateSpecificationsCloseIsPriceBySid",produces = "application/json;charset=UTF-8")
    @ApiImplicitParam(name = "SpecificationsId",value = "商品规格id",dataType = "String",required = true)

    public ApiResult updateSpecificationsCloseIsPriceBySid (@RequestParam Integer SpecificationsId){
        //关闭特价，置为0
        Integer isPrice=0;
        //关闭特价 ，享受优惠卷置为0，不享受
        Integer isCoupon=0;
        //关闭特价 特价价格为0；表示无
        Double price=0.0;
        //关闭特价，到期时间为当前时间
        Date endtime=new Date();
        boolean tf=service.updateSpecificationsIsPriceBySid(SpecificationsId,isCoupon,isPrice,price,endtime);
        if (tf==true){
            return ApiResult.success("关闭规格特价成功！");
        }
        return ApiResult.failure("关闭规格特价失败！");
    }


    @ApiOperation(value = "根据商品id查询商品规格（已调试）")
    @PostMapping(value = "/SelectSpecificationsByGid",produces = "application/json;charset=UTF-8")
    @ApiImplicitParam(name = "GoodsId",value = "商品id",dataType = "String",required = true)
    public ApiResult<List<ResultSpecificationByGoodsId>> SelectSpecificationsByGid (@RequestParam Integer GoodsId){
        List<ResultSpecificationByGoodsId> list=service.SelectSpecificationsByGid(GoodsId);
        if (list.size()>0){
            return ApiResult.successWithData("查询成功",list);
        }
        return ApiResult.failure("查询失败");
    }

    @ApiOperation(value = "增加商品（已调试）")
    @PostMapping(value = "/addGoods",produces = "application/json;charset=UTF-8")
   public ApiResult addGoods (@RequestBody addGoodsBean addGoodsBean){

        boolean tf=service.addGoods(addGoodsBean);
        if (tf==true){
            return ApiResult.success("添加成功!");
        }
        return ApiResult.failure("添加失败！");
    }

    @ApiOperation(value = "删除商品（已调试）")
    @PostMapping(value = "/deleteGoods",produces = "application/json;charset=UTF-8")
    @ApiImplicitParam(name = "GoodsId",value = "商品id",dataType = "String",required = true)
    public ApiResult deleteGoods(@RequestParam Integer GoodsId){

        boolean tf=service.deleteGoods(GoodsId);
        if (tf==true){
            return ApiResult.success("删除成功！");
        }
        return ApiResult.failure("删除失败！");
    }

    @ApiOperation(value = "修改商品（已调试）")
    @PostMapping(value = "/updateGoods",produces = "application/json;charset=UTF-8")
    public ApiResult updateGoods(@RequestBody UpdateGoodsBean updateGoodsBean){

        boolean tf=service.updateGoods(updateGoodsBean);
        if (tf==true){
            return ApiResult.success("修改成功！");
        }
        return ApiResult.failure("修改失败！");
    }

    @ApiOperation(value = "修改商品-添加规格（已调试）")
    @PostMapping(value = "/updateGoodsAddSpecificatios",produces = "application/json;charset=UTF-8")
    public ApiResult updateGoodsAddSpecificatios(@RequestBody AddSpecifications addSpecifications){

        boolean tf=service.updateGoodsAddSpecificatios(addSpecifications);
        if (tf==true){
            return ApiResult.success("增加规格成功！");
        }
        return ApiResult.failure("增加规格失败！");
    }

    @ApiOperation(value = "修改商品-删除规格（已调试）")
    @PostMapping(value = "/updateGoodsDeleteSpecificatios",produces = "application/json;charset=UTF-8")
    @ApiImplicitParam(name = "SpecificationsId",value = "商品规格id",dataType = "String",required = true)
    public ApiResult updateGoodsDeleteSpecificatios(@RequestParam Integer SpecificationsId){

        boolean tf=service.updateGoodsDeleteSpecificatios(SpecificationsId);
        if (tf==true){
            return ApiResult.success("删除规格成功！");
        }
        return ApiResult.failure("删除规格失败！");
    }


    @ApiOperation(value = "修改商品-修改封面图片")
    @PostMapping(value = "/updateGoodsUpdateCover",produces = "application/json;charset=UTF-8")
    public ApiResult updateGoodsUpdateCover(@RequestBody GoodsCover goodsCover){

        boolean tf=service.UpdateCover(goodsCover);
        if (tf==true){
            return ApiResult.success("修改成功！");
        }
        return ApiResult.failure("修改失败！");
    }

    @ApiOperation(value = "修改商品-修改详情图片")
    @PostMapping(value = "/updateGoodsUpdateDetailImg",produces = "application/json;charset=UTF-8")
    public ApiResult updateGoodsUpdateDetailImg(@RequestBody GoodsDetailimg goodsDetailimg){

        boolean tf=service.UpdateDetailImg(goodsDetailimg);
        if (tf==true){
            return ApiResult.success("修改成功！");
        }
        return ApiResult.failure("修改失败！");
    }

}
